<template>
  <div>
    <iframe :src="url" frameborder="0"></iframe>
  </div>
</template>

<script>
import { getUser } from "@/api/api.js";

export default {
  name: '',
  data() {
    return {
      code: '',
      url: 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx77dec9047c8c94d9&redirect_uri=https%3A%2F%2Ffygo.zjich.cn%2Fic_heritage_api%2Fwz%2Ffygo%2FgetOpenid&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'
    };
  },
  created() {
    // this.getCode()
  },
  mounted() {

  },

  methods: {//（以vue为例）
    getCode() {
      // this.code = ''
      // // var local = window.location.href // 获取页面url
      // var local = 'https://fygo.zjich.cn/ic_heritage_api'
      // var appid = 'wx77dec9047c8c94d9'
      // // this.code = this.getUrlCode().code // 截取code
      // if (this.code == null || this.code === '') { // 如果没有code，则去请求
      //   window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=https%3A%2F%2Fjsc.wzfeiyi.cn%2FfygoServer%2Fsystem%2Ffygologin%2FgetOpenid&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
      //     console.log("window.location.href", window.location.href);
      //     // http://192.168.2.186:8080/#/code1
      //     // https://jsc.wzfeiyi.cn/fygo/#/home

      // } else {
      //   console.log(this.code,'9999998888888')

      // }
      // let redirect_uri = encodeURI('https://jsc.wzfeiyi.cn/fygoServer/system/fygologin/getOpenid'),
      // https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx77dec9047c8c94d9&redirect_uri=https://jsc.wzfeiyi.cn/fygo&response_type=code&scope=snsapi_userinfo&state=STATE&uin=MjA3MzE0NTk1&key=22614aef8639902557af6cca9a4cd7c8faba8789c0de4efc2e7cf252281dab19977df81f8fbc3295f12f17f1c014c771&pass_ticket=rLhzLjCcfze95uknu53QqkijVn35468+U2yHe+pqCMfwRq++mN8QervxrP/Fw5XzxHmc0HehBKMn7B7wdW39p23r0tG8xZ0opNDRgg3yZfc=

      

      let code = this.getUrlCode().code; //拿到截取的code
      let redirect_uri = encodeURI('https://jsc.wzfeiyi.cn/fygo')
      let appid = "wx77dec9047c8c94d9";
      let wx_url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
      window.location.href = wx_url;
      if (code == null || code === "") {
        window.location.href = wx_url
      } else {
        this.getOpenid(code) //此方法是将拿到得code传入获取openid得方法内
      }

      // {"msg":"操作成功","code":200,"data":{"country":"","yhid":29,"province":"","city":"","openid":"oV-_e0pwtD5aFkjrc-7lJr2gxo14","sex":0,"nickname":"龍","headimgurl":"https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoLRLdGyNP7TmRn9RBF63unK8aCKOCic65fkJZNArAiazdAQ2DicgDfSKd2xKg1rKiaGGfBvkZKftj0Xw/132"}}
    },
    getUrlCode() {
      var url = location.search;
      var theRequest = new Object();
      if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        var strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
          theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
        }
      }
      return theRequest;
    },
    async getOpenid(code){
      console.log('code===', code);
      let res  = await getUser({
        code: code
      })
      if(res.code == 200){
        sessionStorage.setItem('userlogin', JSON.stringify(res.data))
      }
    }
  }
}
</script>
<style scoped></style>